package edu.gatech.bsbmule.presenter;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import edu.gatech.bsbmule.model.Game;
import edu.gatech.bsbmule.model.Map;
import edu.gatech.bsbmule.view.IGameConfig;
import edu.gatech.bsbmule.view.MainWindow;

/**
 * Presenter for game configuration screen and game and map model
 * 
 * @author BSB
 * @version 1.0 10/26/2013
 */
public class GameConfigPresenter {

	private IGameConfig view;

	public GameConfigPresenter(IGameConfig view) {
		this.view = view;
	}
	
	/**
	 * Retrieve game information from view and setup the game and map
	 * 
	 * @param game game instance
	 * @param map map instance
	 * @param nextPanel the next JPanel MainWindow is going to display
	 */
	public void configureGameAndMap(final Game modelGame
			, final Map modelMap
			, final JPanel nextPanel) {
		view.setOKButtonListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				configureGame(modelGame);
				configureMap(modelMap);
				MainWindow.getInstance().setPanel(nextPanel);
			}
		});
	}
	
	/**
	 * Configure the game according to information in the view
	 * 
	 * @param game game instance
	 */
	private void configureGame(Game modelGame) {
		// Set number of players
		modelGame.setNumberOfPlayers(view.getNumberOfPlayers());
		
		// Set difficulty
		String difficulty = view.getDifficultyLevel();
		int difficultyInt = 0;
		if (difficulty.equals("Beginner")) {
			difficultyInt = Game.DIFFICULTY_BEGINNER;
		} else if (difficulty.equals("Standard")) {
			difficultyInt = Game.DIFFICULTY_STANDARD;
		} else if (difficulty.equals("Tournament")) {
			difficultyInt = Game.DIFFICULTY_TOURNAMENT;
		}
		modelGame.setDifficulty(difficultyInt);
		
		// Set total game turns
		modelGame.setGameTurn(view.getGameTurn());
	}
	
	/**
	 * Configure the map according to information in the view
	 * 
	 * @param map map instance
	 */
	private void configureMap(Map modelMap) {
		int mapType = 0;
		if (view.getTypeOfMap().equals("Standard"))
			mapType = Map.MAP_STANDARD;
		else if (view.getTypeOfMap().equals("Random"))
			mapType = Map.MAP_RANDOM;
		modelMap.generateMap(mapType);
	}
	
}
